package com.inossem.warehousestorage_inossem.adapter.inStorage;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.inossem.warehousestorage_inossem.R;
import com.inossem.warehousestorage_inossem.activity.in_storage.InStorageOtherSelectSpecialInventoryActivity;
import com.inossem.warehousestorage_inossem.bean.inStorage.InStorageOtherSelectSpecialInventoryBean;

import java.util.List;

/**
 * Created by leij on 2017/6/6.
 */

public class OtherSelectSpecialInventoryAdapter extends BaseAdapter {

    private Context mContext;
    private List<InStorageOtherSelectSpecialInventoryBean.SpecialInventoryListBean> mSpecialInventoryList;
    private LayoutInflater inflater;
    private InStorageOtherSelectSpecialInventoryActivity.InStorageOtherSelectSpecialInventoryListener mInStorageOtherSelectSupplierListener;
    private String mSpecialInventoryCode;

    public OtherSelectSpecialInventoryAdapter(Context context, List<InStorageOtherSelectSpecialInventoryBean.SpecialInventoryListBean> specialInventoryList, String specialInventoryCode, InStorageOtherSelectSpecialInventoryActivity.InStorageOtherSelectSpecialInventoryListener inStorageOtherSelectSupplierListener) {
        mContext = context;
        mSpecialInventoryList = specialInventoryList;
        mSpecialInventoryCode = specialInventoryCode;
        mInStorageOtherSelectSupplierListener = inStorageOtherSelectSupplierListener;
        inflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return mSpecialInventoryList.size() + 1;
    }

    @Override
    public Object getItem(int position) {
        return mSpecialInventoryList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (position == 0) {
            convertView = inflater.inflate(R.layout.view_in_storage_other_select_supplier_list_first_item, null);
            EditText supplierCode = (EditText) convertView.findViewById(R.id.supplierCode);
            ImageView search = (ImageView) convertView.findViewById(R.id.search);
            supplierCode.setText(mSpecialInventoryCode);
            supplierCode.setHint(R.string.activity_change_storage_select_special_inventory_please_enter_special_inventory_code);
            search.setOnClickListener(mInStorageOtherSelectSupplierListener.getNoDoubleClickListener(supplierCode));
        } else {
            InStorageOtherSelectSpecialInventoryBean.SpecialInventoryListBean specialInventoryItem = mSpecialInventoryList.get(position - 1);
            ViewHolder viewHolder;
            if (convertView == null || (convertView != null && convertView.getTag() == null)) {
                convertView = inflater.inflate(R.layout.view_in_storage_other_select_supplier_list, null);
                viewHolder = new ViewHolder();
                //初始化控件
                initView(viewHolder, convertView);
                convertView.setTag(viewHolder);
            } else {
                viewHolder = (ViewHolder) convertView.getTag();
            }
            //初始化数据
            initData(mContext, viewHolder, convertView, specialInventoryItem);
        }
        return convertView;
    }

    private void initView(ViewHolder viewHolder, View view) {
        viewHolder.code = (TextView) view.findViewById(R.id.code);
        viewHolder.description = (TextView) view.findViewById(R.id.description);
    }

    private void initData(Context context, ViewHolder viewHolder, View view, InStorageOtherSelectSpecialInventoryBean.SpecialInventoryListBean specialInventoryItem) {
        viewHolder.code.setText(specialInventoryItem.getSpec_stock_code());
        viewHolder.description.setText(specialInventoryItem.getSpec_stock_name());
        view.setOnClickListener(mInStorageOtherSelectSupplierListener.select(specialInventoryItem.getSpec_stock_code(), specialInventoryItem.getSpec_stock_name()));
    }

    private class ViewHolder {
        private TextView code, description;
    }

}
